{__NOLAYOUT__}
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>选择地址</title>
    <link rel="stylesheet" href="https://resource.dinghuo123.com/dist/css/c.css?v=1545736774167">
    <style>
        input.ui_state_highlight {
            outline:none;
            display: inline-block;
            padding: 6px 20px;
            margin-bottom: 0;
            font-size: 14px;
            font-weight: normal;
            line-height: 1.428571429;
            text-align: center;
            white-space: nowrap;
            vertical-align: middle;
            cursor: pointer;
            background-image: none;
            border:0;
            /*border-radius: 2px;*/
            -webkit-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            -o-user-select: none;
            user-select: none;
            color: #fff;
            background-color: #0591aa;
            position:absolute;
            bottom:2px;
            margin-left:-50px;/*div宽度的一半*/
            left:89%;
        }
        input.ui_state_quxiao {
            outline:none;
            display: inline-block;
            padding: 6px 20px;
            margin-bottom: 0;
            font-size: 14px;
            font-weight: normal;
            line-height: 1.428571429;
            text-align: center;
            white-space: nowrap;
            vertical-align: middle;
            cursor: pointer;
            background-image: none;
            border:0;
            /*border-radius: 2px;*/
            -webkit-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            -o-user-select: none;
            user-select: none;
            color: #555555;
            background-color: #e4e9eb;
            position:absolute;
            bottom:2px;
            margin-left:-50px;/*div宽度的一半*/
            left:97%;
        }
        .ui_buttons input:focus{ color: #555555;
            background-color: #d2d9db;
        }
        .ui_buttons input:hover{ color: #555555;
            background-color: #e0e3e4;
        }
        .ui_buttons input:active{ color: #555555;
            background-color: #d2d9db;
        }
        .ui_buttons input[disabled]{  }
        .ui_buttons input.ui_state_highlight:focus{ color: #FFFFFF;
            background-color: #048096;
        }
        .ui_buttons input.ui_state_highlight:hover{ color:#FFFFFF; background-color:#06a4bf;}
        .ui_buttons input.ui_state_highlight:active{ color: #FFFFFF;
            background-color: #048096;
        }
    </style>
</head>
<body>
<div class="alert-content">
    <div class="addressManage pt30 pb30">
        <ul>
            {foreach name="$list" item="val" }
                <li class="template {$val.default == 1 ? 'selected ready' : ''} " style="">
                    <label>
                        <input type="radio" name="addressId" value="{$val.id}" autocomplete="off"   {$val.default == 1 ? 'checked="checked"' : ''}>
                        <input type="hidden" name="address" autocomplete="off" value="{$val.address}">
                        <span class="label">{$val.lxr}</span>&nbsp;&nbsp;
                        <span class="contact">{$val.manger}</span>&nbsp;&nbsp;
                        <span class="address">{$val.city_text}{$val.province_text}{$val.area_text}{$val.address}</span>
                        {if $val.default == 1}
                            <span class="defaultAddressTxt">（默认地址）</span>&nbsp;&nbsp;
                        {/if}
                        <span class="mobile">{$val.mobile}</span>
                    </label>
                    <span class="op {$val.default == 1 ? '' : 'hidden'} ">
                        <a class="edit" data-id="{$val.id}" href="javascript:void(0)">修改</a>
                        <a class="set-default " data-id="{$val.id}"  href="javascript:void(0)">设为默认</a>
                    </span>
                </li>
            {/foreach}
            <li>
                <label class="add">
                    <input type="radio" name="addressId" value="0" autocomplete="off">使用新地址
                </label>
            </li>
        </ul>
        <div class="address-edit-con control-box" style="display: none;">
            <div class="control-group control-group-line">
                <div class="control-label">收货人</div>
                <div class="control-input">
                    <input type="text" class="ui-input" name="contact" autocomplete="off">
                </div>
            </div>
            <div class="control-group control-group-line">
                <div class="control-label">手机</div>
                <div class="control-input">
                    <input type="text" class="ui-input" name="mobile" autocomplete="off">
                </div>
            </div>
            <div class="control-group">
                <div class="control-label">收货地址</div>
                <div class="form-inline control-input selCity" data-toggle="cxselect" data-selects="province,city,area">
                    <select id="province" class="province ui-combo-wrap province-combo" name="province" data-url="/admin/ajax/area"></select>
                    <select id="city" class="city ui-combo-wrap city-combo" name="city" data-url="/admin/ajax/area"></select>
                    <select id="area" class="area ui-combo-wrap district-combo" name="area" data-url="/admin/ajax/area"></select>
                </div>
            </div>

            <div class="control-group">
                <div class="control-label"></div>
                <div class="control-input">
                    <input type="text" class="ui-input" name="address" autocomplete="off">
                </div>
            </div>
            <input type="hidden" id="id" value="">
            <div class="control-group">
                <div class="control-label"></div>
                <div class="control-input">
                    <a class="theme-color save-receive" href="javascript:void(0)">保存</a>
                </div>
            </div>
            <input type="hidden" name="addressId" value="0" autocomplete="off">
        </div>
    </div>
    <div class="ui_buttons">
        <input type="button" value="确定" class="ui_state_highlight">
        <input type="button" value="取消" class="ui_state_quxiao">
    </div>
</div>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="/mobile/js/layer.js"></script>
<script src="/mobile/js/jquery.cxselect.js"></script>
<script>
    $(function () {
      var ids = '{$ids}';
      //切换选中
      $(".template").change(function(){
        $('.address-edit-con').css('display','none');
        $(".template").removeClass('selected');
        $(".op").addClass('hidden');
        $(this).addClass("selected");
        $(this).find(".op").removeClass('hidden');
      });
      //添加
      $('.add').click(function () {
        //显示表单
        reset();//重置表单
        $(".template").removeClass('selected');
        $(".op").addClass('hidden');
        $('.address-edit-con').css('display','block');
        $("[data-toggle='cxselect']").cxSelect({jsonName:'name',jsonValue:'value',jsonSpace:'data'});
      });

      //保存表单
      $('.save-receive').click(function () {
        //验证表单
        var contact = $("input[name='contact']").val(); //获取收货人
        var mobile = $("input[name='mobile']").val();// 获取手机号码
        var address = $("input[name='address']").val();//详细地址
        var province = $("select[name='province']").val();
        var city = $("select[name='city']").val();
        var area = $("select[name='area']").val();
        var id = $("#id").val();
        if(!contact)
        {
          layer.msg('请输入收货人！');
          return false;
        }
        if(mobile.length > 11 || mobile.length < 11)
        {
            layer.msg('请输入有效的11位手机号码！');
            return false;
        }
        if(province == "" || city == "" || area == "")
        {
          layer.msg('请选择收货地区');
          return false;
        }
        if(!address)
        {
          layer.msg('请填写详细地址');
          return false;
        }
        var data = {id:id,lxr:contact,mobile:mobile,address:address,province:province,city:city,area:area};
        //发送表单 保存  表单重置
        $.ajax({
          type: "POST",
          url: "/index/goods/addAddress",
          data: data,
          dataType: "json",
          success: function(data){
            if(data.status > 1)
            {
              layer.msg(data.msg);
              return false
            }
            layer.msg(data.msg);
            //刷新
            window.location.href = '';
            return false;
          }
        });
      });
      //编辑修改
      $('.edit').click(function () {
        reset();//重置表单
        $('.address-edit-con').css('display','block');
        $.ajax({
          type: "POST",
          url: "addressEdit?=",
          data: {id:$(this).attr('data-id')},
          dataType: "json",
          success: function(data){
            $('input[name="contact"]').val(data.lxr);
            $("input[name='address']").val(data.address);
            $("input[name='mobile']").val(data.mobile);
            $("#province").attr('data-value',data.province);
            $("#city").attr('data-value',data.city);
            $("#area").attr('data-value',data.area);
            $("#id").val(data.id);
            $("[data-toggle='cxselect']").cxSelect({jsonName:'name',jsonValue:'value',jsonSpace:'data'});
            return false;
          }
        });
        return false;
      });

      //重置表单
      function reset()
      {
        $('input[name="contact"]').val('');
        $("input[name='address']").val('');
        $("input[name='mobile']").val('');
        $("#province").attr('data-value','');
        $("#city").attr('data-value','');
        $("#area").attr('data-value','');
        $("#id").val('');
      }

      //取消
      $('.ui_state_quxiao').click(function () {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
      });

      //确定
      $('.ui_state_highlight').click(function () {
        var addressId = $("input:radio:checked").val();
        if(addressId > 0)
        {
          $.ajax({
            type: "POST",
            url: "/index/goods/setAddress",
            data: {id:addressId,action:'set',ids:ids},
            dataType: "json",
            success: function(data){
              if(data.status == 0)
              {
                //改变父级元素
                var html = '<input type="hidden" class="addressId yl-add-del"  autocomplete="off" value="'+data.data.id+'">\n' +
                  '<em class="yl-add-del">收货人：</em><span class="contactor yl-add-del">'+data.data.lxr+'</span>\n' +
                  '<em class="yl-add-del"> 联系电话：</em><span class="mobile yl-add-del">'+data.data.mobile+'</span>\n' +
                  '<em class="yl-add-del">收货地址：</em><span class="address yl-add-del">'+data.data.province_text+','+data.data.city_text+','+data.data.area_text+','+data.data.address+'</span>\n' +
                  '<span class="provinceId dn yl-add-del">'+data.data.province+'</span>\n' +
                  '<span class="districtId dn yl-add-del">'+data.data.city+'</span>\n' +
                  '<span class="cityId dn yl-add-del">'+data.data.area+'</span>\n';
                $('.yl-add-del', parent.document).remove();
                $('.address-add', parent.document).after(html);
                $('.address-info li', parent.document).remove();
                $('.address-info ul', parent.document).after(html);
                var html2 = '<input type="hidden" class="addressId yl-add-del" name="row[githouse_id]" autocomplete="off" value="'+data.data.id+'">\n' +
                  '<em class="yl-add-del">收货人：</em><span class="contactor yl-add-del">'+data.data.lxr+'</span>\n' +
                  '<em class="yl-add-del"> 联系电话：</em><span class="mobile yl-add-del">'+data.data.mobile+'</span>\n' +
                  '<em class="yl-add-del">收货地址：</em><span class="address yl-add-del">'+data.data.province_text+','+data.data.city_text+','+data.data.area_text+','+data.data.address+'</span>';
                $('#address-edit-admin', parent.document).before(html2);//
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index); //再执行关闭
                parent.layer.msg(data.msg);
              }

              layer.msg(data.msg);
              return false;
            }
          });
        }
        return false;
      });

      //设置默认地址
      $('.set-default').click(function(){
        var id = $(this).attr('data-id');
        layer.confirm('您是否要设置默认收货地址？', {
          btn: ['确定','取消'] //按钮
        }, function(){
          //确定 ajax
          $.ajax({
            type: "POST",
            url: "/index/goods/setAddress",
            data: {id:id},
            dataType: "json",
            success: function(data){
              layer.msg(data.msg);
              return false;
            }
          });

        }, function(){
          //取消
        });
        return false;

      });
    });
</script>
</body>
</html>